package com.mjh.zhujianju.controller.base;

import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.utils.XTCurrentUser;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.model.base.EnshrineEO;
import com.mjh.zhujianju.service.base.EnshrineService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author liuyang
 * @Description: 收藏
 * @date 2018/8/14 0:28
 */
@RestController
@RequestMapping("/enshrine")
@Api("收藏功能")
public class EnshrineController {

    @Autowired
    private EnshrineService enshrineService;

    @Resource
    private SysUserService userService;

    @ApiOperation(value="收藏-添加", notes="收藏操作，返回：{result:ok ->成功，error->异常，parameter->参数异常，repeat->重复}")
    @ApiImplicitParam(name="收藏实体类", value="EnshrineEO", required = true, dataType = "EnshrineEO")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseEntity<JsonResult> add (@RequestBody EnshrineEO enshrineEO){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null !=userId){
                enshrineEO.setType(BusinessEnum.TYPE_WORKDYNAMIC.getCode());
                enshrineEO.setCreateUserId(userId);

                //获取当前页面的地址，存放到实体中
                String res = enshrineService.addEnshrine(enshrineEO);
                result.setStatus(res);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="收藏-撤回", notes="撤回收藏操作，返回：{result:ok ->成功，error->异常，parameter->参数异常，repeat->重复}")
    @ApiImplicitParam(name="收藏实体类", value="EnshrineEO", required = true, dataType = "EnshrineEO")
    @RequestMapping(value = "/revoke", method = RequestMethod.POST)
    public ResponseEntity<JsonResult> revoke (@RequestBody  EnshrineEO enshrineEO){
        JsonResult result = new JsonResult();
        try {
            //获取当前页面的地址，存放到实体中
            String res = enshrineService.revokeEnshrine(enshrineEO);
            result.setResult(res);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

}
